import React from 'react'
import {useEffect,useState} from 'react'

interface obj{
    key:string,
    name:string,
}
export const isFalse = (value:any)=>(value===0?false:!value)
//console.log('isFalse', isFalse('dsd'))
export const cleanObject = (obj:obj) =>{
    const result = {...obj}
    //const res = Object.assign({},obj)
    type keys=keyof obj;
    Object.keys(result).forEach(key=>{
        
        const value = result[key as keys]
        if(isFalse(value)){
        
            delete result[key as keys]
        }
        return result
    })
}

export const useMount = (callback:()=>void)=>{
    useEffect(() =>{
        callback()
    },[])
}

export const useDebounce = (value:any,delay?:number)=>{
    const [debounceValue ,setDebounceValue] = useState(value)
    useEffect(()=>{
        const timeout = setTimeout(()=>setDebounceValue(value), delay)
        return clearTimeout(timeout)
    },[value,delay])
    return debounceValue
}

// const persons:{name:string,age:number}[] = [
//     {name: 'John', age:12},
//     {name:'Mary',age:13}
// ]
// const useArray = (persons:[])=>{
//     const value = [...persons]
//     const add = (obj:object)=>{
//         return [value, obj]
//     }
//     const removeIndex = (index:number)=>{
//         return value.splice(index, 1)
//     }
//     const clear = ()=>{
//         for(let i=0;i<value.length;i++){
//             value.pop()
//         }
//         return value
//     }
//     return {value, add, removeIndex,clear}
// }

//使用 hook 实现useArray
// const useArray  = (initalState:[])=>{
//     const [value,setValue] = useState(initalState)
//     return{
//         value,
//         setValue,
//         add:(item:{})=>{
//             //@ts-ignore
//             return setValue([...value,item])
//         },
//         clear:()=>{setValue([])},
//         removeIndex:(index:number)=>{
//             const copy = [...value]
//             copy.splice(index,1)
//             //@ts-ignore
//             setValue(copy)
//         }
//     }
// }